package com.aplink.generic.xmlparser;

import java.util.ArrayList;

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import android.text.TextUtils;

import com.aplink.generic.constant.FormDesignConstant;
import com.aplink.generic.constant.TargetContant;
import com.aplink.generic.constant.type.TargetRuleOperator;
import com.aplink.generic.pojo.formdesign.Column;
import com.aplink.generic.pojo.formdesign.PassingValue;
import com.aplink.generic.util.objectrule.Rule;
import com.aplink.generic.util.objectrule.Target;
import com.aplink.generic.util.objectrule.TargetCollection;

public class TargetCollectionPaser {
	public static final String COLUMN = "column";
	public static final String OPERATOR = "operator";
	public static final String PAGE = "page";
	public static final String RULE = "rule";
	public static final String RULES = "rules";
	public static final String TYPE = "type";

	public static TargetCollection parse(final Node nodeFormat) {
		final TargetCollection targetCollection = new TargetCollection();
		Element elementFormat = (Element) nodeFormat;
		NodeList nodeListFormat = elementFormat
		        .getElementsByTagName(TargetContant.TARGETS);
		if (nodeListFormat.getLength() == 0) {
			return targetCollection;
		}
		Node node = nodeListFormat.item(0);
		if (node.hasAttributes()) {
			final String defaultTarget = XmlPaserUtil.getAttrValue(node,
			        TargetContant.DEFAULT_TARGET);
			if (!TextUtils.isEmpty(defaultTarget)) {
				targetCollection.setDefaultActivity(defaultTarget);
			}
		}
		elementFormat = (Element) nodeFormat;
		nodeListFormat = elementFormat
		        .getElementsByTagName(TargetContant.TARGET);
		for (int i = 0; i < nodeListFormat.getLength(); i++) {
			node = nodeListFormat.item(i);
			targetCollection.add(TargetCollectionPaser.parseTarget(node));

		}
		return targetCollection;
	}

	private static PassingValue parsePassingValue(Node node) {
		final PassingValue passingValue = new PassingValue();
		Element element = (Element) node;
		NodeList nodes = element
		        .getElementsByTagName(FormDesignConstant.PASSING_VALUE);
		if (nodes.getLength() == 0) {
			return passingValue;
		}
		node = nodes.item(0);
		if (node.getNodeType() == Node.ELEMENT_NODE) {
			element = (Element) node;
			nodes = element.getElementsByTagName(FormDesignConstant.COLUMN);
			for (int i = 0; i < nodes.getLength(); i++) {
				final Column column = new Column();
				final Node nodeColumn = nodes.item(i);
				String defaultValue = null;
				if (nodeColumn.hasAttributes()) {
					defaultValue = XmlPaserUtil.getAttrValue(nodeColumn,
					        FormDesignConstant.DEFAULT_VALUE);
					column.setDefaultValue(defaultValue);
				}
				final String columnValue = XmlPaserUtil
				        .getNodeValue(nodeColumn);
				column.setColumnName(columnValue);
				passingValue.add(column);
			}
		}
		return passingValue;
	}

	public static ArrayList<Rule> parseRules(final Node node) {
		final ArrayList<Rule> rules = new ArrayList<Rule>();
		NodeList ruleList = ((Element) node)
		        .getElementsByTagName(TargetCollectionPaser.RULES);
		if (ruleList.getLength() == 1) {
			ruleList = ((Element) node)
			        .getElementsByTagName(TargetCollectionPaser.RULE);
			Node ruleNode = null;
			for (int i = 0; i < ruleList.getLength(); i++) {
				ruleNode = ruleList.item(i);
				final Rule rule = new Rule();
				if (ruleNode.hasAttributes()) {
					String value = XmlPaserUtil.getAttrValue(ruleNode,
					        TargetCollectionPaser.OPERATOR);
					if (!TextUtils.isEmpty(value)) {
						rule.setOperator(TargetRuleOperator.get(value));
					}

					value = XmlPaserUtil.getAttrValue(ruleNode,
					        TargetCollectionPaser.COLUMN);
					if (!TextUtils.isEmpty(value)) {
						rule.setColumn(value);
					}

					value = XmlPaserUtil.getNodeValue(ruleNode);
					if (!TextUtils.isEmpty(value)) {
						rule.setValue(value);
					}
				}
				rules.add(rule);
			}
		}
		return rules;
	}

	private static Target parseTarget(final Node node) {
		final Target target = new Target();
		if (node.hasAttributes()) {
			final String value = XmlPaserUtil.getAttrValue(node,
			        TargetCollectionPaser.PAGE);
			if (!TextUtils.isEmpty(value)) {
				target.setPage(value);
			}
		}
		final NodeList ruleList = ((Element) node)
		        .getElementsByTagName(TargetCollectionPaser.RULES);
		if (ruleList.getLength() == 1) {
			final String value = XmlPaserUtil.getAttrValue(ruleList.item(0),
			        TargetCollectionPaser.TYPE);
			if (!TextUtils.isEmpty(value)) {
				target.setType(value);
			}
		}
		target.setRules(TargetCollectionPaser.parseRules(node));

		target.setPassingValue(TargetCollectionPaser.parsePassingValue(node));
		return target;
	}
}
